package com.fz.action;

import com.fz.dao.BookDAO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import lombok.Data;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by zhuang on 2017-09-15.
 */
@Repository
@Data
public class Hello extends ActionSupport {
    private String name;
    @Autowired
    private BookDAO dao;


    //Action()这里不要注解，要不然spring就没有意义了
    public void abd(){
        System.out.println("hello");
        System.out.println(dao.query().size());
    }

    //查询
    public String index(){
        ActionContext.getContext().put("book",dao.query());
        return "index";
    }
    //这个只是做一个视图跳转,打开add那个网页
    public String add(){
        //这个框架要注意，不是说，我写的return哪个，就进入哪个的视图，而是视图name叫success，真正进入哪个视图，看Struts中的配置哦
       //之所以喜欢用success是因为这个在配置里面比较省事
        return "success";
    }
    //增加
    public String save(){
        dao.save(name);
        return "success";
    }

    //删除
    public String delete(){
        HttpServletRequest req=ServletActionContext.getRequest();
        int id=Integer.parseInt(req.getParameter("id"));
        dao.deleteById(id);
        return "success";
    }


//普通的例子
    public void show() throws IOException {
        HttpServletResponse resp= ServletActionContext.getResponse();
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out=resp.getWriter();
        for (int i=0;i<=9;i++){
            out.print(i+"框架测试+<br>");
        }
        System.out.println("helloworld");
    }
}
